package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.vo.TbDistVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TEquipmentBo;
import com.ruoyi.tb.lmt.domain.vo.TEquipmentVo;
import com.ruoyi.tb.lmt.domain.TEquipment;
import com.ruoyi.tb.lmt.mapper.TEquipmentMapper;
import com.ruoyi.tb.lmt.service.ITEquipmentService;

import java.util.*;

/**
 * 高价互换件管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-25
 */
@RequiredArgsConstructor
@Service
public class TEquipmentServiceImpl implements ITEquipmentService {

    private final TEquipmentMapper baseMapper;

    @Autowired
    private TEquipmentMapper equipmentMapper;

    /**
     * 查询高价互换件管理
     */
    @Override
    public TEquipmentVo queryById(Long ID) {
        return baseMapper.selectVoById(ID);
    }

    @Override
    public TEquipmentVo queryByCode(String code) {
        return baseMapper.selectByCode(code);
    }

    /**
     * 查询高价互换件管理列表
     */
    @Override
    public TableDataInfo<TEquipmentVo> queryPageList(TEquipmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TEquipment> lqw = buildQueryWrapper(bo);
        Page<TEquipmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询高价互换件管理列表
     */
    @Override
    public List<TEquipmentVo> queryList(TEquipmentBo bo) {
        LambdaQueryWrapper<TEquipment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TEquipment> buildQueryWrapper(TEquipmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TEquipment> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getEquipmentTypecode()), TEquipment::getEquipmentTypecode, bo.getEquipmentTypecode());
        lqw.eq(StringUtils.isNotBlank(bo.getSubTypecode()), TEquipment::getSubTypecode, bo.getSubTypecode());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkteamId()), TEquipment::getWorkteamId, bo.getWorkteamId());
        lqw.like(StringUtils.isNotBlank(bo.getEquipmentCode()), TEquipment::getEquipmentCode, bo.getEquipmentCode());
        lqw.like(StringUtils.isNotBlank(bo.getEquipmentName()), TEquipment::getEquipmentName, bo.getEquipmentName());
        lqw.eq(StringUtils.isNotBlank(bo.getLmtLength()), TEquipment::getLmtLength, bo.getLmtLength());
        lqw.eq(StringUtils.isNotBlank(bo.getMadeDate()), TEquipment::getMadeDate, bo.getMadeDate());
        lqw.eq(StringUtils.isNotBlank(bo.getBoxNumber()), TEquipment::getBoxNumber, bo.getBoxNumber());
        lqw.eq(bo.getUseDate() != null, TEquipment::getUseDate, bo.getUseDate());
        lqw.eq(StringUtils.isNotBlank(bo.getLmtNumber()), TEquipment::getLmtNumber, bo.getLmtNumber());
        lqw.eq(bo.getStartDgTimes() != null, TEquipment::getStartDgTimes, bo.getStartDgTimes());
        lqw.eq(bo.getStartDate() != null, TEquipment::getStartDate, bo.getStartDate());
        lqw.eq(bo.getEndDgTimes() != null, TEquipment::getEndDgTimes, bo.getEndDgTimes());
        lqw.eq(bo.getEndDate() != null, TEquipment::getEndDate, bo.getEndDate());
        lqw.eq(StringUtils.isNotBlank(bo.getLastRepairNum()), TEquipment::getLastRepairNum, bo.getLastRepairNum());
        lqw.eq(bo.getLastRepairDate() != null, TEquipment::getLastRepairDate, bo.getLastRepairDate());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TEquipment::getStatus, bo.getStatus());
        lqw.eq(bo.getDgTimes() != null, TEquipment::getDgTimes, bo.getDgTimes());
        lqw.eq(bo.getUseTimes() != null, TEquipment::getUseTimes, bo.getUseTimes());
        lqw.eq(bo.getIsdeleted() != null, TEquipment::getIsdeleted, bo.getIsdeleted());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), TEquipment::getRemark, bo.getRemark());
        lqw.eq(StringUtils.isNotBlank(bo.getTag1()), TEquipment::getTag1, bo.getTag1());
        lqw.eq(StringUtils.isNotBlank(bo.getTag2()), TEquipment::getTag2, bo.getTag2());
        lqw.eq(StringUtils.isNotBlank(bo.getTag3()), TEquipment::getTag3, bo.getTag3());
        lqw.eq(StringUtils.isNotBlank(bo.getTag4()), TEquipment::getTag4, bo.getTag4());
        lqw.eq(StringUtils.isNotBlank(bo.getTag5()), TEquipment::getTag5, bo.getTag5());
        lqw.eq(bo.getCreateTime() != null, TEquipment::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TEquipment::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TEquipment::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TEquipment::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getTmpId() != null, TEquipment::getTmpId, bo.getTmpId());
        lqw.eq(bo.getStockindate() != null, TEquipment::getStockindate, bo.getStockindate());
        lqw.eq(StringUtils.isNotBlank(bo.getStockStation()), TEquipment::getStockStation, bo.getStockStation());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), TEquipment::getUnit, bo.getUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getMadeFactory()), TEquipment::getMadeFactory, bo.getMadeFactory());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgValue()), TEquipment::getOrgValue, bo.getOrgValue());
        lqw.eq(StringUtils.isNotBlank(bo.getAssetsCode()), TEquipment::getAssetsCode, bo.getAssetsCode());
        lqw.eq(StringUtils.isNotBlank(bo.getForLmtType()), TEquipment::getForLmtType, bo.getForLmtType());
        return lqw;
    }

    /**
     * 新增高价互换件管理
     */
    @Override
    public Boolean insertByBo(TEquipmentBo bo) {
        TEquipment add = BeanUtil.toBean(bo, TEquipment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改高价互换件管理
     */
    @Override
    public Boolean updateByBo(TEquipmentBo bo) {
        TEquipment update = BeanUtil.toBean(bo, TEquipment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TEquipment entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除高价互换件管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<TEquipmentVo> selectEquipment(String equipmentTypecode, String lmtNumber, String lmtLength, String stockStation, String status, String dateType, Date repairStartDate, Date repairEndDate, String equipmentCode, PageQuery pageQuery) {

        List<TEquipmentVo> result = equipmentMapper.selectEquipment(equipmentTypecode, lmtNumber, lmtLength, stockStation, status, dateType, repairStartDate, repairEndDate, equipmentCode);

        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TEquipmentVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

    @Override
    public List<TEquipmentVo> selectEquipment(String equipmentTypecode, String lmtNumber, String lmtLength, String stockStation, String status, String dateType, Date repairStartDate, Date repairEndDate,String equipmentCode) {
        return equipmentMapper.selectEquipment(equipmentTypecode, lmtNumber, lmtLength, stockStation, status, dateType, repairStartDate, repairEndDate,equipmentCode);
    }

    @Override
    public List<TbDistVo> selectByEquipment(String equipmentTypecode) {

        return equipmentMapper.selectDist(equipmentTypecode);
    }

    @Override
    public TableDataInfo<TEquipmentVo> selectEquByLmtNumber(String lmtNumber, PageQuery pageQuery) {

        List<TEquipmentVo> result = equipmentMapper.selectEquByLmtNumber(lmtNumber);
        //分页
        if (pageQuery.getPageSize() != null && pageQuery.getPageNum() != null) {
            List<TEquipmentVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }
    }

}
